﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Collections;
using WeManageService;


namespace WeManageForm
{
	public partial class PlanForm : Form
	{
		Plan plan = new Plan();
		public int function = 0;
		public PlanForm(int function)
		{
			InitializeComponent();
			TxtAddres.GotFocus += Global.EmptyHintTextBox_GotFocus;
			TxtDescription.GotFocus += Global.EmptyHintTextBox_GotFocus;
			TxtAddres.LostFocus += Global.EmptyHintTextBox_LostFocus;
			TxtDescription.LostFocus += Global.EmptyHintTextBox_LostFocus;
			this.function = function;
			if (function == 0)
			{
				BtnFinish.Text = "确认添加";
			}
			else
			{
				BtnFinish.Text = "确认修改";
				BtnFinish.Location = new Point(654, 354);
			}
			FLPLoopSet.Hide();
		}


		public PlanForm(int function, Plan plan)
		{
			InitializeComponent();
			TxtAddres.GotFocus += Global.EmptyHintTextBox_GotFocus;
			TxtDescription.GotFocus += Global.EmptyHintTextBox_GotFocus;
			TxtAddres.LostFocus += Global.EmptyHintTextBox_LostFocus;
			TxtDescription.LostFocus += Global.EmptyHintTextBox_LostFocus;
			this.plan = plan;
			this.function = function;
			if (function == 1)
			{
				BtnFinish.Text = "确认修改";
				ModifyPanel.Visible = true;
				if (plan.PState == 1 || plan.PState == 2)
				{
					BtnDelay.Visible = false;
					BtnFinishPlan.Visible = false;
					BtnDelete.Visible = false;
					BtnFinish.Visible = false;
					TxtTitle.ReadOnly = true;
					TxtAddres.ReadOnly = true;
					TxtDescription.ReadOnly = true;
					BtnCancel.Text = "确认";
					FLPDelay.Hide();
				}
				LblStateValue.Text = plan.PStateInText;
			}
			if (!plan.Delayable)
			{
				FLPDelay.Hide();
			}
			if (plan.LoopPeriod != 0)
			{
				ChkLoop.Checked = true;
				ChkLoop.Enabled = false;
				RBtnWeek.Visible = true;
				RBtnDay.Visible = true;
				RBtnWeek.Enabled = false;
				RBtnDay.Enabled = false;
				if (plan.LoopPeriod % 7 == 0)
				{
					RBtnWeek.Checked = true;
					TxtLoopWeek.Text = (plan.LoopPeriod / 7).ToString();
					TxtLoopWeek.Enabled = false;
				}
				else
				{
					RBtnDay.Checked = true;
					TxtLoopDay.Text = plan.LoopPeriod.ToString();
					TxtLoopDay.Enabled = false;
				}
			}
			else FLPLoopSet.Hide();

			TxtTitle.Text = plan.Name;
			Global.SetEmptyHintTextBoxValue(TxtAddres, plan.Address);
			Global.SetEmptyHintTextBoxValue(TxtDescription, plan.Description);
			RBtnDelayable.Checked = plan.Delayable;
			RBtnNonDelayable.Checked = !plan.Delayable;
			RBtnDeletable.Checked = plan.Deletable;
			RBtnNonDeletable.Checked = !plan.Deletable;
			ChkLoop.Checked = plan.LoopPeriod != 0;
			DTPStartDay.Text = plan.StartTime.ToString();
			DTPStartTime.Text = plan.StartTime.ToString();
			DTPEndDay.Text = plan.EndTime.ToString();
			DTPEndTime.Text = plan.EndTime.ToString();
			DTPNotifyDay.Text = plan.NotifyTime.ToString();
			DTPNotifyTime.Text = plan.NotifyTime.ToString();
			DTPDelayEndDay.Text = plan.EndTime.ToString();
			DTPDelayEndTime.Text = plan.EndTime.ToString();
			DTPDelayNotifyDay.Text = plan.NotifyTime.ToString();
			DTPDelayNotifyTime.Text = plan.NotifyTime.ToString();
			ChkLoop.Enabled = false;
			RBtnDeletable.Enabled = false;
			RBtnNonDeletable.Enabled = false;
			RBtnDelayable.Enabled = false;
			RBtnNonDelayable.Enabled = false;
			DTPStartDay.Enabled = false;
			DTPStartTime.Enabled = false;
			DTPEndDay.Enabled = false;
			DTPEndTime.Enabled = false;
			DTPNotifyDay.Enabled = false;
			DTPNotifyTime.Enabled = false;
			//FLPLoopSet.Hide();
		}

		//是否循环 选中时
		private void ChkLoop_CheckedChanged(object sender, EventArgs e)
		{
			if (ChkLoop.Checked)
			{
				FLPLoop.BorderStyle = BorderStyle.FixedSingle;
				FLPLoopSet.Visible = true;
			}
			else
			{
				FLPLoop.BorderStyle = BorderStyle.None;
				foreach (object o in FLPLoopSet.Controls)
				{
					if (o is RadioButton)
					{
						RadioButton r = (RadioButton)o;
						r.Checked = false;
					}
					if (o is TextBox)
					{
						TextBox r = (TextBox)o;
						r.Text = "1";
					}
				}
				FLPLoopSet.Visible = false;
			}
		}


		private void RBtnDay_CheckedChanged(object sender, EventArgs e) //按天循环 选中时
		{
			if (RBtnDay.Checked)
			{
				TxtLoopDay.Visible = true;
				LblDays.Visible = true;
			}
			else
			{
				TxtLoopDay.Visible = false;
				LblDays.Visible = false;
			}
		}

		private void RBtnWeek_CheckedChanged(object sender, EventArgs e)//按周循环 选中时
		{
			if (RBtnWeek.Checked)
			{
				TxtLoopWeek.Visible = true;
				LblWeeks.Visible = true;
			}
			else
			{
				TxtLoopWeek.Visible = false;
				LblWeeks.Visible = false;
			}
		}

		private void PanelDelaySet_Click(object sender, PaintEventArgs e)
		{
			if (RBtnDelayable.Checked)
			{
				LblDelayable.Tag = true;
			}
			else if (RBtnNonDelayable.Checked)
			{
				LblDelayable.Tag = false;
			}
			else
			{
				MessageBox.Show("请选择");
			}
			MessageBox.Show(LblDelayable.Tag.ToString());
		}

		private void BtnFinish_Click(object sender, EventArgs e)
		{
			if (function == 0)
			{
				BtnFinish.Text = "确认添加";
				int PState = 0;
				DateTime StartTime = new DateTime(DTPStartDay.Value.Year,
					DTPStartDay.Value.Month,
					DTPStartDay.Value.Day,
					DTPStartTime.Value.Hour,
					DTPStartTime.Value.Minute,
					DTPStartTime.Value.Second
					);//开始时间
				DateTime NotifyTime = new DateTime(DTPNotifyDay.Value.Year,
					DTPNotifyDay.Value.Month,
					DTPNotifyDay.Value.Day,
					DTPNotifyTime.Value.Hour,
					DTPNotifyTime.Value.Minute,
					DTPNotifyTime.Value.Second
					);//提醒时间
				DateTime EndTime = new DateTime(DTPEndDay.Value.Year,
					DTPEndDay.Value.Month,
					DTPEndDay.Value.Day,
					DTPEndTime.Value.Hour,
					DTPEndTime.Value.Minute,
					DTPEndTime.Value.Second
					);//结束时间

				Plan plan = new Plan();
				plan.Name = TxtTitle.Text;
				plan.Description = Global.GetEmptyHintTextBoxValue(TxtDescription);
				plan.Address = Global.GetEmptyHintTextBoxValue(TxtAddres);
				plan.StartTime = StartTime;
				plan.NotifyTime = NotifyTime;
				plan.EndTime = EndTime;
				plan.Delayable = RBtnDelayable.Checked;
				plan.Deletable = RBtnDeletable.Checked;
				plan.UserName = Global.UserName;
				plan.PState = PState;

				if (ChkLoop.Checked)
				{
					if (TxtTitle.Text == "")
					{
						MessageBox.Show("任务标题不可为空", "", MessageBoxButtons.OK, MessageBoxIcon.Warning);
					}
					else if (!(RBtnDelayable.Checked || RBtnNonDelayable.Checked))
					{
						MessageBox.Show("请选择任务是否可推迟", "", MessageBoxButtons.OK, MessageBoxIcon.Warning);
					}
					else if (!(RBtnNonDeletable.Checked || RBtnDeletable.Checked))
					{
						MessageBox.Show("请选择任务是否可删除", "", MessageBoxButtons.OK, MessageBoxIcon.Warning);
					}
					else if (DTPEndDay.Value.Date < DTPStartDay.Value.Date || (DTPEndDay.Value.Date == DTPStartDay.Value.Date && DTPEndTime.Value.Hour < DTPStartTime.Value.Hour)
					   || (DTPEndDay.Value.Date == DTPStartDay.Value.Date && DTPEndTime.Value.Hour == DTPStartTime.Value.Hour && DTPEndTime.Value.Minute <= DTPStartTime.Value.Minute))
					{
						MessageBox.Show("结束时间早于或等于开始时间，请重新设置", "", MessageBoxButtons.OK, MessageBoxIcon.Warning);
					}
					else if (DTPNotifyDay.Value.Date > DTPEndDay.Value.Date || (DTPNotifyDay.Value.Date == DTPEndDay.Value.Date && DTPNotifyTime.Value.Date > DTPEndTime.Value.Date))
					{
						MessageBox.Show("提醒时间晚于结束时间，请重新设置", "", MessageBoxButtons.OK, MessageBoxIcon.Warning);
					}
					else if (RBtnDay.Checked && int.TryParse(TxtLoopDay.Text, out int loopDay))
					{
						plan.LoopPeriod = loopDay;
						PlanManager.AddPlan(plan);
						this.Close();
					}
					else if (RBtnWeek.Checked && int.TryParse(TxtLoopWeek.Text, out int loopWeek))
					{
						plan.LoopPeriod = loopWeek * 7;
						PlanManager.AddPlan(plan);
						this.Close();
					}
					else
					{
						MessageBox.Show("请为循环任务设置循环周期", "", MessageBoxButtons.OK, MessageBoxIcon.Warning);
					}
				}
				else
				{
					if (TxtTitle.Text == "")
					{
						MessageBox.Show("任务标题不可为空", "", MessageBoxButtons.OK, MessageBoxIcon.Warning);
					}
					else if (!(RBtnDelayable.Checked || RBtnNonDelayable.Checked))
					{
						MessageBox.Show("请选择任务是否可推迟", "", MessageBoxButtons.OK, MessageBoxIcon.Warning);
					}
					else if (!(RBtnNonDeletable.Checked || RBtnDeletable.Checked))
					{
						MessageBox.Show("请选择任务是否可删除", "", MessageBoxButtons.OK, MessageBoxIcon.Warning);
					}
					else if (DTPEndDay.Value.Date < DTPStartDay.Value.Date || (DTPEndDay.Value.Date == DTPStartDay.Value.Date && DTPEndTime.Value.Hour < DTPStartTime.Value.Hour)
					   || (DTPEndDay.Value.Date == DTPStartDay.Value.Date && DTPEndTime.Value.Hour == DTPStartTime.Value.Hour && DTPEndTime.Value.Minute <= DTPStartTime.Value.Minute))
					{
						MessageBox.Show("结束时间早于或等于开始时间，请重新设置", "", MessageBoxButtons.OK, MessageBoxIcon.Warning);
					}
					else if (DTPNotifyDay.Value.Date > DTPEndDay.Value.Date || (DTPNotifyDay.Value.Date == DTPEndDay.Value.Date && DTPNotifyTime.Value.Date > DTPEndTime.Value.Date))
					{
						MessageBox.Show("提醒时间晚于结束时间，请重新设置", "", MessageBoxButtons.OK, MessageBoxIcon.Warning);
					}
					else
					{
						PlanManager.AddPlan(plan);
						this.Close();
					}
				}
			}
			else
			{
				plan.Name = TxtTitle.Text;
				plan.Address = Global.GetEmptyHintTextBoxValue(TxtAddres);
				plan.Description = Global.GetEmptyHintTextBoxValue(TxtDescription);
				if (MessageBox.Show("确认修改该任务信息吗", "", MessageBoxButtons.OKCancel, MessageBoxIcon.Information) == DialogResult.OK)
				{
					PlanManager.ModifyPlan(plan);
					MessageBox.Show("修改成功！", "");
					this.Close();
				}
			}
		}

		private void BtnDelete_Click(object sender, EventArgs e)
		{
			if (MessageBox.Show("确认删除该任务吗？", "", MessageBoxButtons.OKCancel, MessageBoxIcon.Information) == DialogResult.OK)
			{
				PlanManager.DeletePlan(plan);
				MessageBox.Show("任务已删除！", "");
				this.Close();
			}
		}

		private void BtnCancel_Click(object sender, EventArgs e)
		{
			this.Close();
		}

		private void BtnDelay_Click(object sender, EventArgs e)
		{
			int DelayYear = 0;
			int DelayMonth = 0;
			int DelayWeek = 0;
			int DelayDay = 0;
			if (DelayRecorder.State == false)
			{
				MessageBox.Show("可用推迟次数已用完", "推迟失败", MessageBoxButtons.OK, MessageBoxIcon.Warning);
			}
			else if (ChkDelay.Checked)
			{
				if (int.TryParse(TxtDelayYear.Text, out DelayYear) || int.TryParse(TxtDelayMonth.Text, out DelayMonth) || int.TryParse(TxtDelayWeek.Text, out DelayWeek) || int.TryParse(TxtDelayDay.Text, out DelayDay))
				{
					if (int.TryParse(TxtDelayYear.Text, out DelayYear))
					{
						DateTime NewEndTime = new DateTime(plan.EndTime.Year + DelayYear,
							plan.EndTime.Month,
							plan.EndTime.Day,
							plan.EndTime.Hour,
							plan.EndTime.Minute,
							plan.EndTime.Second
							);
						DateTime NewNotifyTime = new DateTime(plan.EndTime.Year + DelayYear,
							plan.NotifyTime.Month,
							plan.NotifyTime.Day,
							plan.NotifyTime.Hour,
							plan.NotifyTime.Minute,
							plan.NotifyTime.Second
							);
						plan.EndTime = NewEndTime;
						plan.NotifyTime = NewNotifyTime;
					}
					else if (int.TryParse(TxtDelayMonth.Text, out DelayMonth))
					{
						DateTime NewEndTime = new DateTime(plan.EndTime.Year,
							plan.EndTime.Month + DelayMonth,
							plan.EndTime.Day,
							plan.EndTime.Hour,
							plan.EndTime.Minute,
							plan.EndTime.Second
						);
						DateTime NewNotifyTime = new DateTime(plan.EndTime.Year,
							plan.NotifyTime.Month + DelayMonth,
							plan.NotifyTime.Day,
							plan.NotifyTime.Hour,
							plan.NotifyTime.Minute,
							plan.NotifyTime.Second
							);
						plan.EndTime = NewEndTime;
						plan.NotifyTime = NewNotifyTime;
					}
					else if (int.TryParse(TxtDelayWeek.Text, out DelayWeek))
					{
						DateTime NewEndTime = new DateTime(plan.EndTime.Year,
							plan.EndTime.Month,
							plan.EndTime.Day + DelayWeek * 7,
							plan.EndTime.Hour,
							plan.EndTime.Minute,
							plan.EndTime.Second
						);
						DateTime NewNotifyTime = new DateTime(plan.EndTime.Year,
							plan.NotifyTime.Month,
							plan.NotifyTime.Day, +DelayWeek * 7,
							plan.NotifyTime.Hour,
							plan.NotifyTime.Minute,
							plan.NotifyTime.Second
							);
						plan.EndTime = NewEndTime;
						plan.NotifyTime = NewNotifyTime;
					}
					else if (int.TryParse(TxtDelayDay.Text, out DelayDay))
					{
						DateTime NewEndTime = new DateTime(plan.EndTime.Year,
							plan.EndTime.Month,
							plan.EndTime.Day + DelayDay,
							plan.EndTime.Hour,
							plan.EndTime.Minute,
							plan.EndTime.Second
						);
						DateTime NewNotifyTime = new DateTime(plan.EndTime.Year,
							plan.NotifyTime.Month,
							plan.NotifyTime.Day + DelayDay,
							plan.NotifyTime.Hour,
							plan.NotifyTime.Minute,
							plan.NotifyTime.Second
							);
						plan.EndTime = NewEndTime;
						plan.NotifyTime = NewNotifyTime;
					}
					if (MessageBox.Show("确认推迟该任务吗？", "", MessageBoxButtons.OKCancel, MessageBoxIcon.Information) == DialogResult.OK)
					{
						PlanManager.ModifyPlan(plan);
						MessageBox.Show("推迟成功！", "");
						DelayRecorder.Delay();
					}
				}
				else
				{
					MessageBox.Show("请完善推迟信息", "", MessageBoxButtons.OK, MessageBoxIcon.Warning);
				}
			}
			else
			{
				DateTime NewEndTime = new DateTime(DTPDelayEndDay.Value.Year,
					DTPDelayEndDay.Value.Month,
					DTPDelayEndDay.Value.Day,
					DTPDelayEndTime.Value.Hour,
					DTPDelayEndTime.Value.Minute,
					DTPDelayEndTime.Value.Second
					);
				DateTime NewNotifyTime = new DateTime(DTPDelayNotifyDay.Value.Year,
					DTPDelayNotifyDay.Value.Month,
					DTPDelayNotifyDay.Value.Day,
					DTPDelayNotifyTime.Value.Hour,
					DTPDelayNotifyTime.Value.Minute,
					DTPDelayNotifyTime.Value.Second
					);
				if (NewEndTime.Date <= plan.EndTime.Date)
				{
					MessageBox.Show("新的推迟日期必须晚于原来的推迟日期，请修改", "", MessageBoxButtons.OK, MessageBoxIcon.Warning);
				}
				else if (NewNotifyTime.Date > NewEndTime.Date)
				{
					MessageBox.Show("新的提醒时间必须早于新的结束时间，请修改", "", MessageBoxButtons.OK, MessageBoxIcon.Warning);
				}
				else if (NewEndTime < plan.EndTime.Date)
				{
					MessageBox.Show("新的结束时间应该晚于原先的结束时间，请修改", "", MessageBoxButtons.OK, MessageBoxIcon.Warning);
				}
				else if (NewNotifyTime < plan.NotifyTime.Date)
				{
					MessageBox.Show("新的提醒时间应该晚于原先的结束时间，请修改", "", MessageBoxButtons.OK, MessageBoxIcon.Warning);
				}
				else if (MessageBox.Show("确认推迟该任务吗？还有" + DelayRecorder.Count + "次推迟机会", "", MessageBoxButtons.OKCancel, MessageBoxIcon.Information) == DialogResult.OK)
				{
					plan.EndTime = NewEndTime;
					plan.NotifyTime = NewNotifyTime;
					PlanManager.ModifyPlan(plan);
					MessageBox.Show("推迟成功!", "");
					DelayRecorder.Delay();
				}
			}
		}

		private void ChkDelay_CheckedChanged(object sender, EventArgs e)
		{
			if (ChkDelay.Checked)
			{
				FLPQuickDelay.BorderStyle = BorderStyle.FixedSingle;
				FLPDelaySet.Visible = true;
			}
			else
			{
				FLPQuickDelay.BorderStyle = BorderStyle.None;
				foreach (Object o in FLPDelaySet.Controls)
				{
					if (o is RadioButton)
					{
						RadioButton r = (RadioButton)o;
						r.Checked = false;
					}
					if (o is TextBox)
					{
						TextBox r = (TextBox)o;
						r.Text = "1";
					}
				}
				FLPDelaySet.Visible = false;
			}
		}

		private void RBtnDelayYear_CheckedChanged(object sender, EventArgs e)
		{
			if (RBtnDelayYear.Checked)
			{
				TxtDelayYear.Visible = true;
				LblDelayYear.Visible = true;
			}
			else
			{
				TxtDelayYear.Visible = false;
				LblDelayYear.Visible = false;
			}
		}

		private void RBtnDelayMonth_CheckedChanged(object sender, EventArgs e)
		{
			if (RBtnDelayMonth.Checked)
			{
				TxtDelayMonth.Visible = true;
				LblDelayMonth.Visible = true;
			}
			else
			{
				TxtDelayMonth.Visible = false;
				LblDelayMonth.Visible = false;
			}
		}

		private void RBtnDelayWeek_CheckedChanged(object sender, EventArgs e)
		{
			if (RBtnDelayWeek.Checked)
			{
				TxtDelayWeek.Visible = true;
				LblDelayWeek.Visible = true;
			}
			else
			{
				TxtDelayWeek.Visible = false;
				LblDelayWeek.Visible = false;
			}
		}

		private void RBtnDelayDay_CheckedChanged(object sender, EventArgs e)
		{
			if (RBtnDelayDay.Checked)
			{
				TxtDelayDay.Visible = true;
				LblDelayDay.Visible = true;
			}
			else
			{
				TxtDelayDay.Visible = false;
				LblDelayDay.Visible = false;
			}
		}


		private void BtnFinishPlan_Click(object sender, EventArgs e)
		{
			if (DateTime.Now > plan.EndTime)
			{
				MessageBox.Show("任务已超时，无法完成任务", "", MessageBoxButtons.OK, MessageBoxIcon.Warning);
			}
			else
			{
				PlanFinishForm form = new PlanFinishForm(plan);
				this.Close();
				form.ShowDialog();
			}
		}
	}
}
